angular.module("starter.services", [])
    .factory("Chats", function () {
        var chats = [{
            id: 0,
            name: 'Ben Sparrow',
            lastText: 'You on your way?',
            face: 'https://pbs.twimg.com/profile_images/514549811765211136/9SgAuHeY.png'
        }, {
            id: 1,
            name: 'Max Lynx',
            lastText: 'Hey, it\'s me',
            face: 'https://avatars3.githubusercontent.com/u/11214?v=3&s=460'
        }, {
            id: 2,
            name: 'Adam Bradleyson',
            lastText: 'I should buy a boat',
            face: 'https://pbs.twimg.com/profile_images/479090794058379264/84TKj_qa.jpeg'
        }, {
            id: 3,
            name: 'Perry Governor',
            lastText: 'Look at my mukluks!',
            face: 'https://pbs.twimg.com/profile_images/598205061232103424/3j5HUXMY.png'
        }, {
            id: 4,
            name: 'Mike Harrington',
            lastText: 'This is wicked good ice cream.',
            face: 'https://pbs.twimg.com/profile_images/578237281384841216/R3ae1n61.png'
        }];

        return {
            all: function () {
                return chats;
            },
            remove: function (chat) {
                chats.splice(chats.indexOf(chat), 1);
            },
            get: function (chatId) {
                for (var i = 0; i < chats.length; i++) {
                    if (chats[i].id === parseInt(chatId)) {
                        return chats[i];
                    }
                }
                return null;
            }
        };
    })

    .factory('Phone', function ($window, $q, $ionicLoading, $timeout, $rootScope) {
        return {
            platform: function () {
                if (ionic.Platform.isIOS()) {
                    return 'ios';
                }
                if (ionic.Platform.isAndroid()) {
                    return 'android';
                }
                return 'web';
            },
            version: function () {
                //return mySettings.appVersion;
            },
            setItem: function (key, value) {
                var storage = window.localStorage;
                storage.setItem(key, value);
            },
            getItem: function (key) {
                var storage = window.localStorage;
                return storage.getItem(key);
            },
            toast: function (msg, duration, position) {
                if (!duration)
                    duration = 'short';
                if (!position)
                    position = 'center';

                // PhoneGap? Use native:
                if ($window.plugins) {
                    if ($window.plugins.toast)
                        $window.plugins.toast.show(msg, duration, position,
                            function (a) {
                            }, function (err) {
                            });
                    return;
                }

                //if ($cordovaToast) {
                //  $cordovaToast.showLongCenter(msg);
                //  return;
                //}

                // �� fallback / customized $ionicLoading:
                $ionicLoading.show({
                    template: msg + "-$ionicLoading",
                    noBackdrop: true,
                    duration: (duration == 'short' ? 700 : 3000)
                });

            },
            unique: function (arr) {
                var newArr = [];
                for (var i = 0; i < arr.length; i++) {
                    if (newArr.indexOf(arr[i]) == -1) {
                        newArr.push(arr[i]);
                    }
                }
                return newArr;
            },
            stopPlay: function (file) {
                var filePath = "";
                if (device.platform === "iOS") {
                    filePath = "documents://";
                }
                var playMedia = new Media(filePath + file, function () {
                }, function () {
                });
                playMedia.stop();
            },
            playAudio: function (file) {
                this.stopPlay();
                console.log('PlayAudio: ' + file);

                var filePath = "";
                if (device.platform === "iOS") {
                    filePath = "documents://";
                }

                var playMedia = new Media(filePath + file);
                if (playMedia) {
                    if (this.platform() == "ios") {
                        var iOSPlayOptions = {
                            numberOfLoops: 1,
                            playAudioWhenScreenIsLocked: false
                        }
                        playMedia.play(options); // iOS only!
                    } else {
                        playMedia.play();
                    }
                }
            },
            startRecorder: function (file) {
                var mediaRec = new Media(file);
                mediaRec.startRecord();
                return mediaRec;
            },
            endRecorder: function (media) {
                if (media != null) {
                    media.stopRecord();
                    media = null;
                }
            }
        }
    })

    .factory('Local', function ($window, $q, $ionicLoading, mySettings, $timeout, $rootScope, $cordovaToast) {
        return {
            platform: function () {
                if (ionic.Platform.isIOS()) {
                    return 'ios';
                }
                if (ionic.Platform.isAndroid()) {
                    return 'android';
                }
                return 'web';
            },
            version: function () {
                return mySettings.appVersion;
            },
            setItem: function (key, value) {
                var storage = window.localStorage;
                storage.setItem(key, value);
            },
            getItem: function (key) {
                var storage = window.localStorage;
                return storage.getItem(key);
            },
            isValidPhoneNumber: function (phone) {
                if (parseInt(phone) && String(phone).length >= 11) {
                    return true;
                }
                return false;
            },
            toast: function (msg, duration, position) {
                if (!duration)
                    duration = 'short';
                if (!position)
                    position = 'top';

                // PhoneGap? Use native:
                //if ($window.plugins) {
                //    if ($window.plugins.toast)
                //        $window.plugins.toast.show(msg, duration, position,
                //                                   function (a) { }, function (err) { });
                //    return;
                //}

                //if($cordovaToast) {
                //    $cordovaToast.showLongCenter(msg);
                //    $cordovaToast.show(msg, 'long', 'center');
                //    return;
                //}

                // … fallback / customized $ionicLoading:
                $ionicLoading.show({
                    template: msg,
                    noBackdrop: true,
                    duration: (duration == 'short' ? 1500 : 2500)
                });
            },
            unique: function (arr) {
                var newArr = [];
                for (var i = 0; i < arr.length; i++) {
                    if (newArr.indexOf(arr[i]) == -1) {
                        newArr.push(arr[i]);
                    }
                }
                return newArr;
            },
            setAnonymousRandomResultToGo: function () {// called random portfolio before login
                var storage = window.localStorage;
                storage.setItem("anonymous_random_to_go", "1");
                console.log("anonymous_random_to_go: set");
            },
            removeAnonymousRandomResultToGo: function () {// called random portfolio after login redirection
                var storage = window.localStorage;
                storage.removeItem("anonymous_random_to_go");
                console.log("anonymous_random_to_go removed");
            },
            anonymousRandomResultToGo: function () {// called random portfolio after login redirection
                var storage = window.localStorage;
                return storage.getItem("anonymous_random_to_go");
            },

            setAnonymousBydayResultToGo: function () {// called byday portfolio before login
                var storage = window.localStorage;
                storage.setItem("anonymous_byday_to_go", "1");
                console.log("anonymous_byday_to_go: set");
            },
            removeAnonymousBydayResultToGo: function () {// called byday portfolio after login redirection
                var storage = window.localStorage;
                storage.removeItem("anonymous_byday_to_go");
                console.log("anonymous_byday_to_go removed");
            },
            anonymousBydayResultToGo: function () {// called byday portfolio after login redirection
                var storage = window.localStorage;
                return storage.getItem("anonymous_byday_to_go");
            },

            setAnonymousBymonthResultToGo: function () {// called bymonth portfolio before login
                var storage = window.localStorage;
                storage.setItem("anonymous_bymonth_to_go", "1");
                console.log("anonymous_bymonth_to_go: set");
            },
            removeAnonymousBymonthResultToGo: function () {// called bymonth portfolio after login redirection
                var storage = window.localStorage;
                storage.removeItem("anonymous_bymonth_to_go");
                console.log("anonymous_bymonth_to_go removed");
            },
            anonymousBymonthResultToGo: function () {// called bymonth portfolio after login redirection
                var storage = window.localStorage;
                return storage.getItem("anonymous_bymonth_to_go");
            },
            // 旧API的anonymous token
            setAnonymousToken: function (tk) {
                var storage = window.localStorage;
                storage.setItem("atoken", tk);
                console.log("anonymous token: " + tk + " set");
            },
            anonymousToken: function () {
                var storage = window.localStorage;
                return storage.getItem("atoken");
            },
            // 新API的anonymous token
            setNewAnonymousToken: function (tk) {
                var storage = window.localStorage;
                storage.setItem("newatoken", tk);
                console.log("new anonymous token: " + tk + " set");
            },
            newAnonymousToken: function () {
                var storage = window.localStorage;
                var newToken = storage.getItem("newatoken");
                return newToken || this.anonymousToken();
            },
            setUsername: function (us) {
                var storage = window.localStorage;
                storage.setItem("username", us);
                console.log("username: " + us + " set");
            },
            username: function () {
                var storage = window.localStorage;
                return storage.getItem("username");
            },
            // 旧API的token
            setToken: function (us) {
                var storage = window.localStorage;
                storage.setItem("token", us);
                console.log("token: " + us + " set");
            },
            token: function () {
                var storage = window.localStorage;
                return storage.getItem("token");
            },
            // 新API的token
            setNewToken: function (us) {
                var storage = window.localStorage;
                storage.setItem("newtoken", us);
                console.log("token: " + us + " set");
            },
            newToken: function () {
                var storage = window.localStorage;
                var newToken = storage.getItem("newtoken");
                return newToken || this.token();
            },
            setPayType: function (pay_type) {
                var storage = window.localStorage;
                storage.setItem("pay_type", pay_type);
            },
            /*customerId为用户唯一ID，如UserCode，用于绑定用户银行卡相关认证信息之后再次支付时传入认证支付接口*/
            setToBePayObj: function (customerId, peiZiJinE, baoZhengJinE, liXi, toBeCharged) {
                var storage = window.localStorage;
                storage.setItem("PeiZiCustomerId", customerId);
                storage.setItem("PeiZiJinE", peiZiJinE);
                storage.setItem("PeiZiBaoZhengJinE", baoZhengJinE);
                storage.setItem("PeiZiLiXi", liXi);
                storage.setItem("PeiZiToBeCharged", toBeCharged);

                console.log("PeiZiCustomerId=" + customerId + ";PeiZiJinE=" + peiZiJinE +
                    ";PeiZiBaoZhengJinE=" + baoZhengJinE + ";PeiZiLiXi=" + liXi + ";" +
                    "PeiZiToBeCharged=" + toBeCharged);
            },
            getToBePayObj: function () {
                var storage = window.localStorage;
                var customerId = storage.getItem("PeiZiCustomerId");
                var peiZiJinE = storage.getItem("PeiZiJinE");
                var baoZhengJinE = storage.getItem("PeiZiBaoZhengJinE");
                var liXi = storage.getItem("PeiZiLiXi");
                var toBeCharged = storage.getItem("PeiZiToBeCharged");

                return {
                    customer_Id: customerId,
                    peizijine: peiZiJinE,
                    baozhengjine: baoZhengJinE,
                    lixi: liXi,
                    to_be_charged: toBeCharged
                };
            },
            getPayType: function () {
                var storage = window.localStorage;
                return storage.getItem("pay_type");
            },
            setPayType: function (pay_type) {
                var storage = window.localStorage;
                storage.setItem("pay_type", pay_type);
            },

            setCertInfoObj: function (cardNo, realName, certNo, certType, bankId, cardBindPhoneNo) {
                var storage = window.localStorage;
                storage.setItem("AccountCardNo", cardNo);
                storage.setItem("AccountRealName", realName);
                storage.setItem("AccountCertNo", certNo);
                storage.setItem("AccountCertType", certType);
                storage.setItem("AccountBankId", bankId);
                storage.setItem("AccountBindPhoneNo", cardBindPhoneNo);
                //storage.setItem("securityCode", securityCode);
                //storage.setItem("securityCodeToken", securityCodeToken);

                console.log("AccountCardNo=" + cardNo + ";AccountRealName=" + realName +
                    ";AccountCertNo=" + certNo + ";AccountCertType=" + certType + ";" +
                    "AccountBankId=" + bankId + ";AccountBindPhoneNo=" + cardBindPhoneNo);
                //";securityCode=" + securityCode + ";securityCodeToken=" + securityCodeToken);
            },
            getCertInfoObj: function () {
                var storage = window.localStorage;
                var cardNo = storage.getItem("AccountCardNo");
                var realName = storage.getItem("AccountRealName");
                var certNo = storage.getItem("AccountCertNo");
                var certType = storage.getItem("AccountCertType");
                var bankId = storage.getItem("AccountBankId");
                var phoneNo = storage.getItem("AccountBindPhoneNo");
                //var securityCodeToken = storage.getItem("securityCodeToken");
                //var securityCode = storage.getItem("securityCode");

                return {
                    card_no: cardNo,
                    real_name: realName,
                    cert_no: certNo,
                    cert_type: certType,
                    bank_id: bankId,
                    bind_phone_no: phoneNo,
                    //security_code: securityCode,
                    //security_code_token: securityCodeToken
                };
            },
            getBankList: function () {
                var bankList = [
                    {bank_id: "ICBC_D_B2C", bank_name: "中国工商银行", bank_logo: "/banklogo/gongshang.gif"},
                    {bank_id: "ABC_D_B2C", bank_name: "中国农业银行", bank_logo: "/banklogo/nongye.gif"},
                    {bank_id: "CMBCD_D_B2C", bank_name: "民生银行", bank_logo: "/banklogo/minsheng.gif"},
                    {bank_id: "CCB_D_B2C", bank_name: "中国建设银行", bank_logo: "/banklogo/jianshe.gif"},
                    {bank_id: "BOCSH_D_B2C", bank_name: "中国银行", bank_logo: "/banklogo/zhongguo.gif"},
                    {bank_id: "CIB_D_B2C", bank_name: "兴业银行", bank_logo: "/banklogo/xingye.gif"},
                    {bank_id: "CEB_D_B2C", bank_name: "光大银行", bank_logo: "/banklogo/guangda.gif"},
                    {bank_id: "CITIC_B2C", bank_name: "中信银行", bank_logo: "/banklogo/zhongxin.gif"},
                    {bank_id: "PINGAN_B2C", bank_name: "平安银行", bank_logo: "/banklogo/pingan.gif"},
                    {bank_id: "COMM_D_B2C", bank_name: "交通银行", bank_logo: "/banklogo/jiaotong.gif"},
                    {bank_id: "POSTGC_D_B2C", bank_name: "中国邮政", bank_logo: "/banklogo/youzheng.gif"}
                    //{ bank_id: "CMB_D_B2C", bank_name: "招商银行", bank_logo: "/banklogo/zhaohang.gif" },


                    // { bank_id: "SDB_B2C", bank_name: "深圳发展银行", bank_logo: "/banklogo/shenfa.gif" },
                    //{ bank_id: "GDB_D_B2C", bank_name: "广东发展银行", bank_logo: "/banklogo/guangfa.gif" },


                    // { bank_id: "BJRCB_B2C", bank_name: "北京农村商业银行", bank_logo: "/banklogo/nongcunshangye.gif" },
                    // { bank_id: "FDB_B2C", bank_name: "富滇银行", bank_logo: "/banklogo/hudianBank.gif" },
                    // { bank_id: "HXB_B2C", bank_name: "华夏银行", bank_logo: "/banklogo/huaxia.gif" },
                    // { bank_id: "HZCB_B2C_B2C", bank_name: "杭州银行", bank_logo: "/banklogo/hangzhou.gif" },
                    // { bank_id: "NBBANK_B2C", bank_name: "宁波银行", bank_logo: "/banklogo/ningbo.gif" },
                    // { bank_id: "SHBANK_B2C", bank_name: "上海银行", bank_logo: "/banklogo/shanghaibank.gif" },
                    // { bank_id: "SPDB_B2C", bank_name: "浦东发展银行", bank_logo: "/banklogo/shangpufa.gif" }
                ];
                return bankList;
            },
            setSelectedBank: function (bankId, bankName) {
                var storage = window.localStorage;
                storage.setItem("selectedBankId", bankId);
                storage.setItem("selectedBankName", bankName);
                console.log("selectedBankId: " + bankId + " set;selectedBankName:" + bankName + " set.");
            },
            getSelectedBank: function () {
                var storage = window.localStorage;
                var bankId = storage.getItem("selectedBankId");
                var bankName = storage.getItem("selectedBankName");

                return {
                    bank_id: bankId,
                    bank_name: bankName
                };
            },
            setSecurityCodeResult: function (outTradeNo, securityCode, securityCodeToken, signKey) {
                var storage = window.localStorage;
                storage.setItem("outTradeNo", outTradeNo);
                storage.setItem("securityCode", securityCode);
                storage.setItem("securityCodeToken", securityCodeToken);
                storage.setItem("signKey", signKey);

                console.log("outTradeNo=" + outTradeNo + ";securityCode=" +
                    securityCode + ";securityCodeToken=" + securityCodeToken +
                    ";signKey=" + signKey);
            },
            getSecurityCodeResult: function () {
                var storage = window.localStorage;
                var outTradeNo = storage.getItem("outTradeNo");
                var securityCodeToken = storage.getItem("securityCodeToken");
                var securityCode = storage.getItem("securityCode");
                var signKey = storage.getItem("signKey");

                return {
                    out_trade_no: outTradeNo,
                    security_code: securityCode,
                    security_code_token: securityCodeToken,
                    signKey: signKey
                };
            }
        };
    })

// 逐步替换旧接口
    .factory('NewPort', function (RHTTP, newServerHost, apiSettings, Local) {
        return {
            placeOrder: function () {
                return RHTTP.h(newServerHost + apiSettings.apiUirPortfolioPlaceOrder,
                    {
                        version: apiSettings.appVersion,
                        token: Local.token(),
                        phoneNo: Local.username()
                    });
            },

            getUserBankCards: function () {
                return RHTTP.h(newServerHost + apiSettings.apiUriGetUserBankCards, {
                    version: apiSettings.appVersion,
                    token: Local.token(),
                    username: Local.username(),
                });
            },
            ProjectInvest: function (id, name, fund) {
                return RHTTP.h(newServerHost + apiSettings.apiUriProjectInvest, {
                    token: Local.newToken(),
                    phoneNo: Local.username(),
                    projectId: id,
                    projectName: name,
                    fund: fund
                });
            },
        };
    })
    .factory('PersonalInfo', function ($http, mySettings, serverHost, RHTTP, Local) {

        var ports = [];
        var isIdentifier = false;

        return {
            syncVersion: function () {// directly to user account, not order
                return RHTTP.h(serverHost + mySettings.apiUriPayByUserAccount,
                    {
                        version: mySettings.appVersion,
                        platform: Local.platform()
                    });
            },
            get_token: function () {
                return RHTTP.h(serverHost + mySettings.apiUriAnonymousToken, {version: mySettings.appVersion});
            },
            register: function (phone, password, auth_code, nick_name, invite_code) {
                return RHTTP.h(serverHost + mySettings.apiUriRegister,
                    {
                        version: mySettings.appVersion,
                        username: phone,
                        password: password,
                        auth_code: auth_code,
                        nickname: nick_name,
                        invite_code: invite_code
                    });
            },
            auth_code_register: function (phone) {
                return RHTTP.h(serverHost + mySettings.apiUriAuthCodeRegister, {
                    version: mySettings.appVersion,
                    username: phone
                });
            },
            auth_code_check: function (phone, auth_code) {
                return RHTTP.h(serverHost + mySettings.apiUriAuthCodeCheck, {
                    version: mySettings.appVersion,
                    username: phone,
                    auth_code: auth_code
                });
            },
            recover: function (phone, auth_code, password) {
                return RHTTP.h(serverHost + mySettings.apiUriRecover,
                    {
                        version: mySettings.appVersion,
                        auth_code: auth_code,
                        new_password: password,
                        username: phone
                    });
            },
            reset_password: function (phone, auth_code, old_password, new_password) {
                return RHTTP.h(serverHost + mySettings.apiUriReset,
                    {
                        version: mySettings.appVersion,
                        token: Local.token(),
                        auth_code: auth_code,
                        old_password: old_password,
                        new_password: new_password,
                        username: phone
                    });
            },
            auth_code_recover: function (phone) {
                return RHTTP.h(serverHost + mySettings.apiUriAuthCodeRecover, {
                    version: mySettings.appVersion,
                    username: phone
                });
            },
            login: function (username, password) {
                console.log("Using api: " + serverHost);
                return RHTTP.h(serverHost + mySettings.apiUriLogin,
                    {
                        version: mySettings.appVersion,
                        username: username,
                        password: password,
                        token: Local.anonymousToken()
                    });
            },
            signOff: function () {
                var storage = window.localStorage;
                storage.removeItem("anonymous_random_to_go");
                storage.removeItem("username");
                storage.removeItem("token");
            },
            logout: function () {
                return RHTTP.h(serverHost + mySettings.apiUriLogout,
                    {
                        version: mySettings.appVersion,
                        username: Local.username(),
                        token: Local.token()
                    });
            },
            fetch: function () {
                return RHTTP.h(serverHost + mySettings.apiUriPersonalSetting, {
                    version: mySettings.appVersion,
                    token: Local.token(),
                    username: Local.username()
                });
            },
            changeWebName: function (nickname) {
                return RHTTP.h(serverHost + mySettings.apiUriChangeNickName, {
                    version: mySettings.appVersion,
                    token: Local.token(),
                    username: Local.username(),
                    nickname: nickname
                });
            },
            setIdentifer: function (val) {
                isIdentifier = val;
            },
            getIdentifer: function () {
                return isIdentifier;
            },
            realname: "",
            idcardcode: ""

        };
    })
// 新接口获取个人信息
    .factory('NewPersonalInfo', function (RHTTP, apiSettings, newServerHost, Local) {
        return {
            get_token: function () {
                return RHTTP.h(newServerHost + apiSettings.NapiUriGetToken, {
                    version: apiSettings.appVersion
                });
            },
            login: function (phone, password) {
                console.log("login using api: " + newServerHost);
                return RHTTP.h(newServerHost + apiSettings.NapiUriLogin, {
                    token: Local.newAnonymousToken(),
                    phoneNo: phone,
                    password: password
                })
            }
        };
    })
    .factory('RHTTP', function ($http, mySettings, serverHost, Local) {
        return {
            h: function (url, data) {
                var http = $http({
                    method: 'POST',
                    url: url,
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                    transformRequest: function (obj) {
                        var str = [];
                        for (var p in obj)
                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                        return str.join("&");
                    },
                    data: data//{version:mySetting.appVersion, username:phone, password:password, auth_code:auth_code, nickname:nick_name}
                });
                var tempSucc = http.success;
                http.success = function (succFn) {
                    return tempSucc(function (ret) {
                        if (ret && ret.result == -999 && ret.msg) {
                            Local.toast(ret.msg, 2);
                        } else if (ret && ret.msg && ret.is_show_msg) {
                            succFn(ret);
                            Local.toast(ret.msg, 2);
                        } else {
                            succFn(ret);
                        }
                    });
                };
                return http;
            }
        };
    })

    .factory("EntrustService",function(RHTTP,serverHost,mySettings,Local){
      return {
        QueryEntrustsByIserId:function(){
          return RHTTP.h(serverHost + mySettings.apiUriQueryEntrustsByUserId,
              {
                version: mySettings.appVersion,
                token: Local.token(),
                username: Local.username()
              });
          },
        AddEntrustMessage:function(msgType,msg){
          return RHTTP.h(serverHost + mySettings.apiUriAddEntrustMessage,
              {
                version: mySettings.appVersion,
                token: Local.token(),
                username: Local.username(),
                messageType:msgType,
                message:msg
              });
        }
      }
    })

    .factory("ProductService",function(RHTTP,serverHost,newServerHost,mySettings,Local){
      return {
        GetProductDetail:function(pid,ptype){
            console.log("type:"+ptype);
          if(ptype==1){
            return this.GetLendingProduct(pid);
          }else{
            return this.GetFinancingDetail(pid);
          }
        },
        GetFinancingDetail:function(productId){
          return RHTTP.h(newServerHost + mySettings.apiUriGetFinancingDetail,
              {
                version: mySettings.appVersion,
                token: Local.token(),
                username: Local.username(),
                ProjectID:productId
              });
        },
        GetLendingProduct:function(productId){
          return RHTTP.h(serverHost + mySettings.apiUriGetLendingProduct,
              {
                version: mySettings.appVersion,
                token: Local.token(),
                username: Local.username(),
                productid:productId
              });
        },
        AddProductOrder:function(amount,productId,productType,remark){
          return RHTTP.h(serverHost + mySettings.apiUriAddProductOrder,
              {
                version: mySettings.appVersion,
                token: Local.token(),
                username: Local.username(),
                amount:amount,
                productid:productId,
                productType:productType,
                remark:remark
              });
        }
      };
    })


    .factory('Port', function (RHTTP, $q, $timeout, serverHost, mySettings, Local) {

      return {
        getSecurityCodeByBankCardId :function(){
          var storage = window.localStorage;
          return storage.getItem("pay_type");
        },
        getSecurityCodeByBankCardId:function(id,amount,payType,bankcode){
          return RHTTP.h(serverHost + mySettings.apiUriGetSecurityCodeByBankCardId,
              {
                version: mySettings.appVersion,
                token: Local.token(),
                username: Local.username(),
                amount:amount,
                cardId:id,
                pay_type:payType,
                bankcode:bankcode
              });
        },
        getSecurityCode: function (customerId,amount,cardNo,realName,certNo,certType,bankCode,bankName,phoneNo,payType) {
          return RHTTP.h(serverHost + mySettings.apiUriGetSecurityCode,
              {
                version: mySettings.appVersion,
                token: Local.token(),
                username: Local.username(),
                customer_id: customerId,
                card_no: cardNo,
                real_name: realName,
                cert_no: certNo,
                cert_type: certType,
                amount: amount,
                bank_code: bankCode,
                bank_name:bankName,
                card_bind_phone_no: phoneNo,
                pay_type:payType
              });
        },
        queryUserBills: function (start, pagecapacity) {// directly to user account, not order
          return RHTTP.h(serverHost + mySettings.apiUriQueryUserBills,
              {
                version: mySettings.appVersion,
                token: Local.token(),
                start: start,
                pagecount: pagecapacity,
                username: Local.username()
              });
        },
        userChargeBeifu: function (total_fee, dynamic_code, dynamic_code_token, out_trade_code,
                                   customer_id, card_no, real_name, cert_no, cert_type, bank_code, card_bind_phone_no,signKey) {// directly to user account, not order
          return RHTTP.h(serverHost + mySettings.apiUriChargeBeiFu,
              {
                version: mySettings.appVersion,
                token: Local.token(),
                total_fee: total_fee,
                username: Local.username(),
                dynamic_code: dynamic_code,
                dynamic_code_token: dynamic_code_token,
                out_trade_code: out_trade_code,
                customer_id: customer_id,
                card_no: card_no,
                real_name: real_name,
                cert_no: cert_no,
                cert_type: cert_type,
                bank_code: bank_code,
                card_bind_phone_no: card_bind_phone_no,
                signKey: signKey
              });
        },
        getWithdrawInfo: function (){
          // 检查用户是否有提现申请
          return RHTTP.h(serverHost + mySettings.apiUriGetUserWithdrawInfo,
              {
                version: mySettings.appVersion,
                token: Local.token(),
                username: Local.username()
              });
        },
        userWithdraw: function (tixianjine, yonghuxingming, tixianzhanghao, tixianzhanghaoshuoming, zhanghaoliexing) {// directly to user account, not order
          return RHTTP.h(serverHost + mySettings.apiUriUserWithdraw,
              {
                version: mySettings.appVersion,
                token: Local.token(),
                yonghuxingming: yonghuxingming,
                tixianjine: tixianjine,
                tixianzhanghao: tixianzhanghao,
                tixianzhanghaoshuoming: tixianzhanghaoshuoming,
                zhanghaoliexing: zhanghaoliexing,
                username: Local.username()
              });
        },
        queryUserAmount: function () {// directly to user account, not order
          return RHTTP.h(serverHost + mySettings.apiUriQueryUserAmount,
              {
                version: mySettings.appVersion,
                token: Local.token(),
                username: Local.username()
              });
        },
        getUserAssets:function()
        {
          return RHTTP.h(serverHost + mySettings.apiUserAsses,
              {
                version: mySettings.appVersion,
                token: Local.token(),
                username: Local.username()
              });
        },
        query_type:1
      };
    })


    .factory("BankCards", function (RHTTP, serverHost, mySettings, Local) {
        return {
            queryBankCards: function () {
                return RHTTP.h(serverHost + mySettings.apiUriQueryBankCards, {
                    version: mySettings.appVersion,
                    token: Local.token(),
                    username: Local.username()
                });
            },
            addBankCard: function (bankname, cardnumber, realname, userid, isDefaultCard) {
                return RHTTP.h(serverHost + mySettings.apiUriAddBankCard, {
                    version: mySettings.appVersion,
                    token: Local.token(),
                    username: Local.username(),
                    bankname: bankname,
                    cardnumber: cardnumber,
                    realname: realname,
                    userid: userid,
                    isdefaultcard: isDefaultCard
                });
            },
            removeBankCard: function (cardNumber) {
                return RHTTP.h(serverHost + mySettings.apiUriRemoveBankCard, {
                    version: mySettings.appVersion,
                    token: Local.token(),
                    username: Local.username(),
                    cardnumber: cardNumber
                });
            },
            setDefaultBankCard: function (cardNumber) {
                return RHTTP.h(serverHost + mySettings.apiUriSetDefaultBankCard, {
                    version: mySettings.appVersion,
                    token: Local.token(),
                    username: Local.username(),
                    cardnumber: cardNumber
                });
            },
            identifier_bind: function (realname, idcardcode) {
                return RHTTP.h(serverHost + mySettings.apiUriIdentifierBind, {
                    version: mySettings.appVersion,
                    token: Local.token(),
                    username: Local.username(),
                    realname: realname,
                    idcardcode: idcardcode
                });
            }
        };
    })
    .service('sharedProperties', function () {
        var hashTable = {};

        return {
            setValue: function (key, value) {
                hashTable[key] = value;
            },
            getValue: function (key) {
                return hashTable[key];
            }
        }
    })
    .factory('productList', function (RHTTP, newServerHost, apiSettings, Local) {
        return {
            getProductList: function () {
                //console.log(newServerHost + apiSettings.apiUriGetProductList);
                return RHTTP.h(newServerHost + apiSettings.apiUriGetProductList, {});
            },


        };
    })
    .factory('WMP', function (RHTTP, newServerHost, apiSettings, Local) {
        return {
            getProjectList: function (page, roll) {
                return RHTTP.h(newServerHost + apiSettings.apiUriGetWMPList, {
                    token: Local.newToken(),
                    phoneNo: Local.username(),
                    page: page,
                    roll: roll
                });
            },
            getProjectDetail: function (id) {
                return RHTTP.h(newServerHost + apiSettings.apiUriGetWMPDetail, {
                    token: Local.newToken(),
                    phoneNo: Local.username(),
                    id: id
                });

            },
            ProjectInvest: function (id, name, fund) {
                return RHTTP.h(newServerHost + apiSettings.apiUriProjectInvest, {
                    token: Local.newToken(),
                    phoneNo: Local.username(),
                    projectId: id,
                    projectName: name,
                    fund: fund
                });
            },
            getProjectInvestList: function () {
                return RHTTP.h(newServerHost + apiSettings.apiUriProjectInvestList, {
                    token: Local.newToken(),
                    phoneNo: Local.username()
                })
            },
            ProjectInvestWithdraw: function (projectId, outId) {
                return RHTTP.h(newServerHost + apiSettings.apiUriProjectInvestWithdraw, {
                    token: Local.newToken(),
                    phoneNo: Local.username(),
                    projectId: projectId,
                    outId: outId
                })
            }
        };
    });

